package com.xiaojian.pick.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.xiaojian.pick.entity.ApplyBond;
import com.xiaojian.pick.entity.Category;
import com.xiaojian.pick.entity.Commodity;
import com.xiaojian.pick.entity.CommodityImg;
import com.xiaojian.pick.mapper.CommodityMapper;
import com.xiaojian.pick.page.CommodityCustom;
import com.xiaojian.pick.service.ApplyBondService;
import com.xiaojian.pick.service.CommodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.sound.midi.Soundbank;
import java.util.List;
import java.util.Map;

/**
 * @author 小贱
 * @date 2020/10/13 - 16:38
 */
@Service
public class CommodityServiceImpl extends ServiceImpl<CommodityMapper, Commodity> implements CommodityService {

    @Autowired
    private CommodityMapper commodityMapper;
    @Resource
    @Lazy
    private ApplyBondService applyBondService;


    @Override
    public Commodity findById(Integer id) {
        return commodityMapper.findById(id);
    }

    @Override
    public List<Commodity> findByUserId(Integer id) {
        return commodityMapper.findByUserId(id);
    }

    @Override
    public List<Commodity> findByCategoryId(Integer id) {
        return commodityMapper.findByCategoryId(id);
    }

    @Override
    public List<Commodity> findAll() {
        return commodityMapper.findAll();
    }

    @Override
    public List<Commodity> findByParam(CommodityCustom commodityCustom, Integer page, Integer pageSize) {
        if(page != null && pageSize != null){
            PageHelper.startPage(page,pageSize);
        }
        return commodityMapper.findByParam(commodityCustom);
    }

    @Override
    public Long getCount(CommodityCustom commodityCustom) {
        return commodityMapper.getCount(commodityCustom);
    }

    @Override
    public int addCommodity(Commodity commodity) {
        return commodityMapper.addCommodity(commodity);
    }

//    @Override
//    public Commodity getCommodityImgList(String id) {
//        return commodityMapper.getCommodityImgList(id);
//    }

    @Override
    public int deleteByid(Integer id) {
        return commodityMapper.deleteByid(id);
    }

    @Override
    public int realDelCommodity(Integer id) {
        return commodityMapper.realDelCommodity(id);
    }

    @Override
    public int updateCoverImgOfCommodity(Commodity commodity) {
        return commodityMapper.updateCoverImgOfCommodity(commodity);
    }

    @Override
    public int updateCommodityState(Integer state, Integer id) {
        return commodityMapper.updateCommodityState(state,id);
    }

    @Override
    public int addCommodityClick(Integer id) {
        return commodityMapper.addCommodityClick(id);
    }

    @Override
    public List<Commodity> getUserPublish(Commodity commodity) {
        return commodityMapper.getUserPublish(commodity);
    }

    @Override
    public List<Commodity> getSwiperCommodity() {
        return commodityMapper.getSwiperCommodity();
    }

    @Override
    public List<Commodity> findByPage(Commodity commodity,Integer page,Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        return commodityMapper.findByPage(commodity);
    }

    @Override
    public List<Commodity> search(String content,Integer page,Integer pageSize) {

        PageHelper.startPage(page,pageSize);
        return commodityMapper.search(content);
    }

    @Override
    public Page<Commodity> queryMyCommodityList(Integer userId, Integer state, Integer page, Integer limit) {
        QueryWrapper<Commodity> queryWrapper = new QueryWrapper<Commodity>()
                .eq("user_id", userId)
                .eq("state", state);
        Page<Commodity> page1 = this.page(new Page<Commodity>().setCurrent(page).setSize(limit), queryWrapper);
        int count = this.count(queryWrapper);
        page1.setTotal(count);
        List<Commodity> records = page1.getRecords();
        records.forEach(record -> {
            Map<String, Object> map = applyBondService.list(new ApplyBond().setCommodity(record.getId()));
            record.setApplyBonds((List<ApplyBond>) map.get("list"));
        });
        page1.setRecords(records);
        return page1;
    }

    @Override
    @Transactional(propagation =  Propagation.REQUIRED)
    public void increaseComment(Integer cid) {
        this.update(new UpdateWrapper<Commodity>().setSql(" comment_count = comment_count + 1 ")
                .eq("id", cid));
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void increaseSupport(Integer commodityId) {
        this.update(new UpdateWrapper<Commodity>().setSql(" support_count = support_count + 1 ")
                .eq("id", commodityId));
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void decreaseSupport(Integer commodityId) {
        this.update(new UpdateWrapper<Commodity>().setSql(" support_count = support_count - 1 ")
                .eq("id", commodityId).gt("support_count", 0));
    }

}
